package org.ikun.security_demo;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class HelloController {

    /**
     * 如果访问这个路径，Security处理器执行链中将不包含SessionManagementFilter
     * @param httpSession
     * @return
     */
    @GetMapping("/web/hello")
    public String hello(HttpSession httpSession){
        //登录成功的用户对象获取方式
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal);
//        new Thread(() -> {
//            Object principal1 = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//            System.out.println(principal1);
//        }).start();
        new Thread(() -> {
            SecurityContext spring_security_context = (SecurityContext)httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
            System.out.println(spring_security_context.getAuthentication().getPrincipal());
        }).start();
        return "web hello";
    }

    /**
     * 如果访问这个路径，Security处理器执行链中包含SessionManagementFilter
     * @return
     */
    @GetMapping("/v1/hello")
    public String hello2(){
        return "v1 hello";
    }
}
